package org.java.mathgame.screens;



import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import org.java.mathgame.MathGame;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.ArrayList;



public class GameSettingsScreen implements Screen {
    private MathGame game;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    private Stage stage;
    private TextButton tChallengeButton;
    private TextButton fClearingButton;
    private TextButton mediumButton;
    private TextButton hardButton;
    private TextButton brutalButton;
    private TextButton easyButton;
    private CheckBox negCheck;
    private int difficulty = 1;
    private int gameMode = 1;
    private boolean negative = false;

    // constructor to keep a reference to the main Game class
    public GameSettingsScreen(final MathGame game){
        this.game = game;
        ArrayList<TextButton.TextButtonStyle> buttonStyles = new ArrayList<TextButton.TextButtonStyle>();
        spriteBatch = new SpriteBatch();
        font = new BitmapFont();
        stage = new Stage();
        Table rootTable = new Table();
        rootTable.setFillParent(true);
        rootTable.setWidth(Gdx.graphics.getWidth());
        rootTable.setBackground(new ButtonBackground(new Color(238 / 255f, 238 / 255f, 238 / 255f, 1)));
        rootTable.pad(10);
        Table menuTable = new Table();
        TextButton.TextButtonStyle gridButtonStyle = new TextButton.TextButtonStyle();
        TextButton.TextButtonStyle operatorButtonStyle;
        Color[] colors = new Color[]{
                new Color(210 / 255f, 57 / 255f, 59 / 255f, 1),
                new Color(58 / 255f, 143 / 255f, 208 / 255f, 1),
                new Color(59 / 255f, 209 / 255f, 112 / 255f, 1),
                new Color(208 / 255f, 147 / 255f, 58 / 255f, 1)};
        Color[] darkColors = new Color[]{
                new Color(162 / 255f, 9 / 255f, 11 / 255f, 1),
                new Color(10 / 255f, 95 / 255f, 160 / 255f, 1),
                new Color(11 / 255f, 161 / 255f, 62 / 255f, 1),
                new Color(162 / 255f, 98 / 255f, 10 / 255f, 1)};
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        BitmapFont buttonFont = generator.generateFont((int) (Gdx.graphics.getHeight() / 20f));
        //generator.dispose();
        for (int i = 0; i < 4; i++) {
            TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
            style.font = buttonFont;
            style.pressedOffsetX = 1;
            style.pressedOffsetY = -1;
            style.font.setColor(Color.WHITE);
            style.up = new ButtonBackground(colors[i]);
            style.checked = new ButtonBackground(darkColors[i]);
            buttonStyles.add(style);
        }

        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));
        //FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        infoStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        TextButton.TextButtonStyle labelStyle = new TextButton.TextButtonStyle(infoStyle);
        labelStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/20f));
        Label.LabelStyle backStyle = new Label.LabelStyle();
        backStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        backStyle.background = new ButtonBackground(new Color(0,0,0,0));

        Label backLabel = new Label("  <",backStyle);


        TextButton backButton = new TextButton("Game Settings ",infoStyle);

        backButton.getLabel().remove();
        backButton.clear();
        backButton.add(backLabel).expandX().left();
        backButton.add(backButton.getLabel());
        backButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(game.mainMenuScreen);
            }
        });

        Table gameModeTable = new Table();
        gameModeTable.setBackground(new ButtonBackground(new Color(203/255f,203/255f,203/255f,1)));

        TextButton modeLabel = new TextButton("Game Mode",labelStyle);
        modeLabel.clear();
        modeLabel.add(modeLabel.getLabel()).expandX().left().padLeft(5);
        tChallengeButton = new TextButton("Time Challenge",buttonStyles.get(0));
        tChallengeButton.clear();
        tChallengeButton.add(tChallengeButton.getLabel()).expandX().left().padLeft(5);
        tChallengeButton.setChecked(true);
        tChallengeButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                fClearingButton.setChecked(false);
                tChallengeButton.setChecked(true);
                gameMode = 1;
            }
        });

        fClearingButton = new TextButton("Field Clearing",buttonStyles.get(0));
        fClearingButton.clear();
        fClearingButton.add(fClearingButton.getLabel()).expandX().left().padLeft(5);
        fClearingButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                tChallengeButton.setChecked(false);
                fClearingButton.setChecked(true);
                gameMode = 2;
            }
        });
        gameModeTable.add(modeLabel).expandX().left().width(Gdx.graphics.getWidth() / 2f);
        gameModeTable.row();
        gameModeTable.add(tChallengeButton).expandX().fillX().space(5);
        gameModeTable.row();
        gameModeTable.add(fClearingButton).expandX().fillX().space(5);
        gameModeTable.pad(10,5,10,5);
        Table difficultyTable = new Table();
        difficultyTable.setBackground(new ButtonBackground(new Color(203/255f,203/255f,203/255f,1)));

        TextButton difficultyLabel = new TextButton("Difficulty",labelStyle);
        difficultyLabel.clear();
        difficultyLabel.add(difficultyLabel.getLabel()).expandX().left().padLeft(5);

        Label.LabelStyle helpStyle = new Label.LabelStyle();
        helpStyle.background = new ButtonBackground(new Color(0,0,0,0));
        helpStyle.font = generator.generateFont(Gdx.graphics.getHeight()/25);
        helpStyle.fontColor = new Color(203/255f,203/255f,203/255f,1);

        easyButton = new TextButton("Easy",buttonStyles.get(1));
        easyButton.clear();
        easyButton.add(easyButton.getLabel()).expandX().left().padLeft(5);
        easyButton.add(new Label("(+ -)x1 1-10",helpStyle)).padRight(5);
        easyButton.setChecked(true);
        easyButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                easyButton.setChecked(true);
                mediumButton.setChecked(false);
                hardButton.setChecked(false);
                brutalButton.setChecked(false);
                difficulty = 1;
            }
        });
        mediumButton = new TextButton("Medium",buttonStyles.get(1));
        mediumButton.clear();
        mediumButton.add(mediumButton.getLabel()).expandX().left().padLeft(5);
        mediumButton.add(new Label("(+ - × ÷)x1 1-15",helpStyle)).padRight(5);
        mediumButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                easyButton.setChecked(false);
                mediumButton.setChecked(true);
                hardButton.setChecked(false);
                brutalButton.setChecked(false);
                difficulty = 2;
            }
        });
        hardButton = new TextButton("Hard",buttonStyles.get(1));
        hardButton.clear();
        hardButton.add(hardButton.getLabel()).expandX().left().padLeft(5);
        hardButton.add(new Label("(+ - × ÷)x2 1-15",helpStyle)).padRight(5);
        hardButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                easyButton.setChecked(false);
                mediumButton.setChecked(false);
                hardButton.setChecked(true);
                brutalButton.setChecked(false);
                difficulty = 3;
            }
        });
        brutalButton = new TextButton("Brutal",buttonStyles.get(1));
        brutalButton.clear();
        brutalButton.add(brutalButton.getLabel()).expandX().left().padLeft(5);
        brutalButton.add(new Label("(+ - × ÷)x2 1-30",helpStyle)).padRight(5);
        brutalButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                easyButton.setChecked(false);
                mediumButton.setChecked(false);
                hardButton.setChecked(false);
                brutalButton.setChecked(true);
                difficulty = 4;
            }
        });
        difficultyTable.add(difficultyLabel).expandX().left().width(Gdx.graphics.getWidth() / 2f);
        difficultyTable.row();
        difficultyTable.add(easyButton).expandX().fillX().space(5);
        difficultyTable.row();
        difficultyTable.add(mediumButton).expandX().fillX().space(5);
        difficultyTable.row();
        difficultyTable.add(hardButton).expandX().fillX().space(5);
        difficultyTable.row();
        difficultyTable.add(brutalButton).expandX().fillX().space(5);
        difficultyTable.pad(10,5,10,5);

        Table negativeTable = new Table();
        negativeTable.setBackground(new ButtonBackground(new Color(203/255f,203/255f,203/255f,1)));
        TextButton negativeButton = new TextButton("Negative Numbers",buttonStyles.get(2));
        CheckBox.CheckBoxStyle chStyle = new CheckBox.CheckBoxStyle();
        Texture texture =  new Texture("data/check_box.png");
        texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
        chStyle.checkboxOn = new SpriteDrawable(new Sprite(texture));
        texture =  new Texture("data/uncheck_box.png");
        texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
        chStyle.checkboxOff = new SpriteDrawable(new Sprite(texture));

        chStyle.font = new BitmapFont(); // will not be used, so doesn't matter
        negCheck = new CheckBox("",chStyle);
        negativeButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                negCheck.toggle();
                negative = !negative;
            }
        });
        for(EventListener listener:negCheck.getListeners()){
            negCheck.removeListener(listener);
        }
        negativeButton.clear();
        negativeButton.add(negativeButton.getLabel()).expandX().left().padLeft(5);
        negativeButton.add(negCheck).height(negativeButton.getHeight()).width(negativeButton.getHeight());
        negCheck.setScale(0.5f, 0.5f);
        negativeTable.add(negativeButton).expandX().fillX().space(5);
        negativeTable.pad(10,5,10,5);

        Texture playTexture = new Texture("data/play.png");
        playTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
        ImageButton playButton = new ImageButton(new SpriteDrawable(new Sprite(playTexture)));
        TextButton startButton = new TextButton("Start",buttonStyles.get(3));
        startButton.clear();
        startButton.add(startButton.getLabel()).expandX().left().padLeft(5);
        startButton.add(playButton).height(startButton.getHeight()*(2/3f)).width(startButton.getHeight()*(2/3f)).expandX().right().padRight(5);
        startButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setDifficulty(difficulty);
                game.setGameMode(gameMode);
                game.setNegative(negative);
                if(game.gameScreen != null)
                    game.gameScreen.dispose();
                game.gameScreen = new GameScreen(game);
                game.setScreen(game.gameScreen);
            }
        });


        rootTable.add(backButton).fillX().expandY().top();
        rootTable.row();
        rootTable.add(gameModeTable).expandX().fillX().expandY().top().space(5);
        rootTable.row();
        rootTable.add(difficultyTable).expandX().fillX().expandY().top().space(5);
        rootTable.row();
        rootTable.add(negativeTable).expandX().fillX().expandY().top().space(5);
        rootTable.row();
        rootTable.add(startButton).expandX().right().expandY().width(Gdx.graphics.getWidth()/3.5f);
        //rootTable.debug();
        //gameModeTable.debug();
        stage.addActor(rootTable);
        Gdx.input.setInputProcessor(stage);
        generator.dispose();

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.5f, 0.3f, 0.6f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        font.setColor(1f, 1f, 1f, 1f);
        /*spriteBatch.begin();
        font.draw(spriteBatch, "mainMenuScreen", Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
        spriteBatch.end();*/
        stage.draw();
        //if (Gdx.input.justTouched()) // use your own criterion here
        //    game.setScreen(game.howToPlayScreen);
        Table.drawDebug(stage);
    }


    @Override
    public void resize(int width, int height) {
    }


    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }


    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }


    @Override
    public void pause() {
    }


    @Override
    public void resume() {
    }


    @Override
    public void dispose() {
        // never called automatically
    }
}